package com.zs.demo.common.util;

import com.zs.demo.common.exception.RestException;
import com.zs.demo.shiro.MyToken;
import com.zs.demo.status.LoginStatus;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Licoy
 * @version 2018/4/28/9:46
 */
public class CommonUtil {

    /**
     * 获取客户端IP
     * @param request
     * @return
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (ip==null || "".equals(ip.trim()) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if("0.0.0.0".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip) || "localhost".equals(ip) || "127.0.0.1".equals(ip)){
            ip = "127.0.0.1";
        }
        return ip;
    }

    public static boolean executeLogin(ServletRequest request){
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String authorization = httpServletRequest.getHeader("Authorization");
        if(authorization==null || "".equals(authorization.trim())){
            throw new RestException(LoginStatus.LOGIN_UN_AUTH);
        }
        MyToken token = new MyToken(authorization,null,null);
        // 提交给realm进行登入，如果错误他会抛出异常并被捕获
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        }catch (DisabledAccountException e){
            if(e.getMessage().equals("verifyFail")){
                throw new RestException(LoginStatus.LOGIN_EXPIRE);
            }
            throw new RestException(e.getMessage(),e);
        }catch (Exception e){
            e.printStackTrace();
            throw new RestException(LoginStatus.LOGIN_FAIL,e);
        }
        // 如果没有抛出异常则代表登入成功，返回true
        return true;
    }

    public static synchronized void executeLogin(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        boolean b = CommonUtil.executeLogin(request);
        if(!b){
            throw new RestException(LoginStatus.LOGIN_EXPIRE);
        }
    }

    public static String getLoginname(){
        PrincipalCollection spc = null;
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals()!=null){
            spc = subject.getPrincipals();
        }
        //判断身份是否为空
        if(spc!=null){
            MyToken myToken = new MyToken();
            BeanUtils.copyProperties(spc.getPrimaryPrincipal(), myToken);
            return myToken.getUsername();
        }

        return "-1";
    }
}
